<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="系统流水" @ok="handleSubmit" width="300"
              :canFullscreen="false"
              :centered="true"
              :destroyOnClose="true"
  >
      <BasicForm @register="registerForm"/>
  </BasicModal>
</template>

<script lang="ts" setup>
    import {ref, computed, unref} from 'vue';
    import {BasicModal, useModalInner} from '/@/components/Modal';
    import {BasicForm, useForm} from '/@/components/Form/index';
    import {bankRunningById, bankBalanceUpdate} from '/@/views/full/bank/types/bank.api';
    // Emits声明
    const emit = defineEmits(['register','success']);
    const isUpdate = ref(true);

    //表单配置
    const [registerForm, {resetFields, setFieldsValue, validate}] = useForm({
      schemas: [
        { label: "", field: "id", component: "Input", show: false },
        {
          label: "收支账户",
          field: "bankId",
          component: "JDictSelectTag",
          componentProps: {
            dictCode: "chee_bank,djm,id@ddlxBank",
            placeholder: " ",
            stringToNumber: true
          },
          required: true,
          dynamicDisabled: true,
        },
        {
          label: "部门账户",
          field: "bankOid",
          component: "JDictSelectTag",
          componentProps: {
            dictCode: "chee_bank,djm,id@ddlxBank",
            placeholder: " ",
            stringToNumber: true
          },
          dynamicDisabled: true,
          ifShow: ({model}) => {
            return model.id != "";
          }
        },
        {
          label: "收支类型",
          field: "sflx",
          component: "JDictSelectTag",
          componentProps: { 
            dictCode: "sflxType", 
            placeholder: " " 
          },
          required: true,
          dynamicDisabled: true,
        },
        {
          label: "记账日期",
          field: "jzsj",
          component: "DatePicker",
          componentProps: { 
            valueFormat: "YYYY-MM-DD",
            style: "width: 110px; padding:4px",
            getPopupContainer: () => document.body,
          },
          required: true
        }, //, rules: [{required: true, type: 'string'}],
        {
          label: "金额",
          field: "je",
          component: "InputNumber",
          componentProps: { placeholder: " " },
          rules: [{ required: true, type: "number" }],
          dynamicDisabled: true,
        },
        {
          label: "收支部门",
          field: "ddlx",
          component: "JDictSelectTag",
          componentProps: {
            dictCode: "ddlxType",
            placeholder: " ",
          },
          dynamicDisabled: true,
        },
        { 
          label: "备注", 
          field: "bz", 
          component: "InputTextArea", 
          componentProps: { placeholder: " ", rows: 8 }, 
          dynamicDisabled: true, 
        }
      ],
      labelWidth: 120,
      baseColProps: {span: 20},
      showActionButtonGroup: false,
    });

    //表单赋值
    const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
        //重置表单
        await resetFields();
        //setModalProps({confirmLoading: false,showCancelBtn:data?.showFooter,showOkBtn:data?.showFooter});
        isUpdate.value = !!data?.isUpdate;
        if (unref(isUpdate)) {
          bankRunningById({id: data.record.xtid}).then(res => {
            setFieldsValue(res);
          })
        }
    });
    //设置标题
    const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
    //表单提交事件
    async function handleSubmit(v) {
        try {
            let values = await validate();
            setModalProps({confirmLoading: true});
            //提交表单
            let params = {
              id: values.id,
              jzsj: values.jzsj,
            }
            await bankBalanceUpdate(params);
            //关闭弹窗
            closeModal();
            //刷新列表
            emit('success', {isUpdate: isUpdate.value, values});
        } finally {
            setModalProps({confirmLoading: false});
        }
    }
</script>

<style lang="less" scoped>
:deep(.ant-input-number .ant-input-number-input) {
  padding: 0 3px !important;
}
:deep(.ant-input-affix-wrapper) {
  padding: 0px !important;
}
</style>